<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="desigination">
        <el-input
          v-model="queryParams.desigination"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['info:networkIpMgmt:add']"
        >添加</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['info:networkIpMgmt:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="networkIpMgmtList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="desigination" />
 <!--     <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="更新人" align="center" prop="updateName" />
      <el-table-column label="预留" align="center" prop="reserved" /> -->
      <el-table-column label="有线网络" align="center" prop="finiteNetwork" />
      <el-table-column label="无线网络" align="center" prop="wirelessNetwork" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['info:networkIpMgmt:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['info:networkIpMgmt:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网络IP管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="9">
          <el-col :span="12">
        <el-form-item label="名称" prop="desigination">
          <el-input v-model="form.desigination" placeholder="请输入名称" />
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>

        <el-divider content-position="center">网络详情信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini"   @click="editTitle">添加
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini"   @click="handleDeleteNetworkDetails">删除
            </el-button>
          </el-col>
        </el-row>

       <el-dialog :title="dialogtitle" :visible.sync="dialogTableVisible" width="650px" append-to-body center>
      <el-form ref="titleform" :model="titleform" :rules="rules" label-width="100px" style="margin-top: 30px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="VLAN" prop="vlan">
              <el-input v-model="titleform.vlan"  placeholder="请输入VLAN" clearable :style="{width: '87%'}">
              </el-input>
            </el-form-item>
          </el-col>
        
          <el-col :span="12">
            <el-form-item label="IP" prop="ip">
              <el-input v-model="titleform.ip"  placeholder="请输入IP" clearable :style="{width: '87%'}">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="start" align="top">
          <el-col :span="12">
            <el-form-item label="Gateway" prop="gateway">
              <el-input v-model="titleform.gateway"  placeholder="请输入Gateway" clearable :style="{width: '87%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Description" prop="description">
              <el-input v-model="titleform.description"  placeholder="请输入Description" clearable :style="{width: '87%'}">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="类型" prop="networkType">
              <el-input v-model="titleform.networkType"  placeholder="请输入类型" clearable :style="{width: '87%'}">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-button type="primary" @click="definiteBtn()" style="margin-left: 500px;margin-top: 20px">确定</el-button>
    </el-dialog>
        <el-table :data="networkDetailsList" :row-class-name="rowNetworkDetailsIndex"  @selection-change="handleNetworkDetailsSelectionChange" ref="networkDetails">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" />
          <el-table-column label="VLAN" prop="vlan" />
    
          
          <el-table-column label="IP" prop="ip" />
            
          <el-table-column label="Gateway" prop="gateway" />
            
          <el-table-column label="Description" prop="description" />
            
          <el-table-column label="类型" prop="networkType" />
          <el-table-column label="修改"  align="center"  class-name="small-padding fixed-width" >
            <template slot-scope="scope"  >
              <el-button
                type="primary"
                size="small"

                @click="delistchange(scope.row)"
              >修 改
              </el-button>
            </template>
          </el-table-column>
           
          
        </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNetworkIpMgmt, getNetworkIpMgmt, delNetworkIpMgmt, addNetworkIpMgmt, updateNetworkIpMgmt, exportNetworkIpMgmt } from "@/api/info/networkIpMgmt";

export default {
  name: "NetworkIpMgmt",
  data() {
    return {
      dialogtitle: '',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedNetworkDetails: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网络IP管理表格数据
      networkIpMgmtList: [],
      // 网络详情表格数据
      networkDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      titleform: {

      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        desigination: null,
        createName: null,
        updateName: null,
        reserved: null,
        finiteNetwork: null,
        wirelessNetwork: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        desigination: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        vlan: [
          {required: true, message: "VLAN不能为空", trigger: "blur"}
        ],
        ip: [
          {required: true, message: "IP不能为空", trigger: "blur"}
        ],
        gateway: [
          {required: true, message: "Gateway不能为空", trigger: "blur"}
        ],
        description: [
          {required: true, message: "Description不能为空", trigger: "blur"}
        ],
        networkType: [
          {required: true, message: "类型不能为空", trigger: "blur"}
        ],
      },
      dialogTableVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询网络IP管理列表 */
    getList() {
      this.loading = true;
      listNetworkIpMgmt(this.queryParams).then(response => {
        this.networkIpMgmtList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        desigination: null,
        createName: null,
        createTime: null,
        updateName: null,
        updateTime: null,
        reserved: null,
        finiteNetwork: null,
        wirelessNetwork: null
      };
      this.networkDetailsList = [];
      this.resetForm("form");
    },
    resets(){
      this.titleform = {
        vlan: null,
        ip: null,
        gateway: null,
        description: null,
        networkType: null,
      }
      this.resetForm("titleform")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加网络IP管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resets();
      const id = row.id || this.ids
      getNetworkIpMgmt(id).then(response => {
        this.form = response.data;
        this.networkDetailsList = response.data.networkDetailsList;
        this.open = true;
        this.title = "修改网络IP管理";
      });
      if(this.$refs.form){
        this.$refs.form.clearValidate();
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.networkDetailsList = this.networkDetailsList;
          if (this.form.id != null) {
            updateNetworkIpMgmt(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNetworkIpMgmt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    editTitle(){
      this.titleform = {}
      this.dialogtitle = "添加网络详情信息"
      this.dialogTableVisible = true
      if(this.$refs.titleform){
        this.$refs.titleform.clearValidate();
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除网络IP管理编号为"' + ids + '"的数据项？').then(function() {
        return delNetworkIpMgmt(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 弹出窗体确定按钮
    definiteBtn() {
      this.$refs["titleform"].validate((valid) =>{
        if(valid){
          if(this.titleform.index == undefined){
            let tempList = [];
            tempList.push(this.titleform);
            this.$modal.msgSuccess("添加成功");
            this.networkDetailsList.push(...tempList)
          }else{
            for(var i = 0;i<this.networkDetailsList.length,i++;){
              if(this.networkDetailsList[i].index == this.titleform.index){
                this.networkDetailsList[i].splice(i,1,1,this.titleform)
              }
            }
            this.$modal.msgSuccess("修改成功");
          }
        }else {
            
            return false;
        }
      this.dialogTableVisible = false;
      })
    },
	/** 网络详情序号 */
    rowNetworkDetailsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 修改按钮 */
    delistchange(row) {
      this.dialogTableVisible = true;
      if(this.$refs.titleform){
        this.$refs.titleform.clearValidate();
      }
    
          this.titleform = row;
          for(var i = 0;i<this.networkDetailsList.length,i++;){
            if(this.networkDetailsList[i].index == row.index){
              this.networkDetailsList[i].splice(i,1,1,this.titleform)
            }
          }
        
    },
    /** 网络详情删除按钮操作 */
    handleDeleteNetworkDetails() {
      if (this.checkedNetworkDetails.length == 0) {
        this.$modal.msgError("请先选择要删除的专案稽核事项详细记录数据");
      } else {
        const networkDetailsList = this.networkDetailsList;
        const checkedNetworkDetails = this.checkedNetworkDetails;
        this.networkDetailsList = networkDetailsList.filter(function (item) {
          return checkedNetworkDetails.indexOf(item.index) == -1
        });
        this.$modal.msgSuccess("删除成功");
      }
    },
    /** 复选框选中数据 */
    handleNetworkDetailsSelectionChange(selection) {
      this.checkedNetworkDetails = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有网络IP管理数据项？').then(() => {
        this.exportLoading = true;
        return exportNetworkIpMgmt(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
